/*
 * Copyright 2008 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.dopdf.demo.controller;

import org.apache.log4j.Logger;
import org.dopdf.demo.utils.Constants;
import org.dopdf.document.PDFDocument;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;
import java.io.InputStream;

/**
 * The demo controller that will show off our do-PDF application.
 *
 * @author Deep Shah
 */
@Controller
public class DemoController {

    /**
     * The log4j logger reference.
     */
    private static final Logger logger = Logger.getLogger(DemoController.class);

    /**
     * The method that will show the flying saucer userguide pdf on the page.
     *
     * @param model the model in which we will populate the pdf identifier.
     * @return String value representing the next view to render.
     * @throws java.io.IOException if the flying saucer pdf is not found.
     */
    @RequestMapping(value = "/pdf/small", method = RequestMethod.GET)
    public String small(final ModelMap model) throws IOException {
        return showPDFViewer(model, new ClassPathResource(Constants.Demo.FLYINGSAUCER_PDF_FILE_NAME).getInputStream());
    }

    /**
     * The method that will show the hibernate reference pdf on the index page.
     *
     * @param model the model in which we will populate the pdf identifier.
     * @return String value representing the next view to render.
     * @throws java.io.IOException if the hibernate reference pdf is not found.
     */
    @RequestMapping(value = "/pdf/big", method = RequestMethod.GET)
    public String big(final ModelMap model) throws IOException {
        return showPDFViewer(model, new ClassPathResource(Constants.Demo.HIBERNATE_PDF_FILE_NAME).getInputStream());
    }

    /**
     * This action method will render the upload view for the user
     * @return String value representing the update view.
     */
    @RequestMapping(value = "/pdf/uploadform", method = RequestMethod.GET)
    public String uploadForm() {
        return "pdf/uploadform";
    }

    /**
     * This action is responsible to do the actual upload of the pdf document.  It will show the uploaded pdf document
     * to the user.
     * @return the index view which shows the uploaded pdf document to the user.
     * @throws java.io.IOException incase if we are not able to get the upload stream.
     */
    @RequestMapping(value = "/pdf/upload", method = RequestMethod.POST)
    public String upload(@RequestParam(value = "pdfDocument") final CommonsMultipartFile uploadedFile,
                         final ModelMap model) throws IOException {
        if(0 == uploadedFile.getSize()) {
            model.put("uploadError", true);
            return "pdf/uploadform";
        }
        if(!Constants.Resource.PDF_CONTENT_TYPE.equals(uploadedFile.getContentType())) {
            model.put("fileTypeError", true);
            return "pdf/uploadform";
        }
        return showPDFViewer(model, uploadedFile.getInputStream());
    }

    /**
     * The private method that will be used to show the pdf viewer for different pdf documents.
     * @param model the model used to populate the pdf document.
     * @param inputStream the pdf stream to show.
     * @return the String value representing the view to render.
     */
    private String showPDFViewer(final ModelMap model, final InputStream inputStream) {
        try {
            final PDFDocument pdfDocument =
                    new PDFDocument(inputStream);
            logger.info("The document id generated: " + pdfDocument.getId());
            model.put("document", pdfDocument);
        } catch (final Exception e) {
            logger.error(e.getMessage(), e);
        }
        return "pdf/index";
    }
}
